<?php

namespace App\Http\Resources\Admin;

use App\Http\Resources\BaseResource;

class RoleResource extends BaseResource
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'permission_id' => $this->when($this->permission_id, $this->permission_id),
            'menus_id' => $this->when($this->menus_id, $this->menus_id),

            // 关联信息
            'permissions' => PermissionResource::collection($this->whenLoaded('permissions')),
            'menus' => MenusResource::collection($this->whenLoaded('menus')),
            'admin_menus' => AdminMenusResource::collection($this->whenLoaded('adminMenus')),

            'created_at' => $this->created_at->format('Y-m-d H:i:s')
        ];
    }

}
